#include "Common/TableDataEx/TeleportCfg.h"
#include "Common//TableData/TeleportBornCfg.h"
#include "Common/SceneDefine.h"
#include "base/core/random.h"
#include "Common/SceneDefine.h"

TeleportCfg::TeleportCfg()
{
	m_mapCampBorn.clear();
}

TeleportCfg::~TeleportCfg()
{

}

bool TeleportCfg::Init()
{
	if (!Process())
	{
		return false;
	}

	return true;
}

void TeleportCfg::UnInit()
{
	m_mapCampBorn.clear();
}

const SET_UINT32 * TeleportCfg::GetMapTeleportBornByCamp(uint32_t mapId, int8_t camp)
{
	MAP_UINT32_MAP_INT8_SET_UINT32::iterator iter = m_mapCampBorn.find(mapId);
	if (iter != m_mapCampBorn.end())
	{
		MAP_INT8_SET_UINT32 &mapCampBorn = iter->second;
		MAP_INT8_SET_UINT32::iterator iterBorn = mapCampBorn.find(camp);
		return (iterBorn != mapCampBorn.end()) ? &iterBorn->second : nullptr;
	}
	return nullptr;
}

bool TeleportCfg::Process()
{
	const TeleportBornCfgMap *pTeleportMapCfg = g_GetTeleportBornCfgTable()->GetTeleportBornCfgMap();
	TeleportBornCfgMap::const_iterator iter = pTeleportMapCfg->begin();
	for (; iter != pTeleportMapCfg->end(); ++iter)
	{
		const TeleportBornCfgInfo &info = iter->second;
		if (ETeleportCamp_Inject == info.camp || ETeleportCamp_Survive == info.camp)
		{
			MAP_UINT32_MAP_INT8_SET_UINT32::iterator iter = m_mapCampBorn.find(info.scene);
			if (iter != m_mapCampBorn.end())
			{
				MAP_INT8_SET_UINT32 &mapCampBorn = iter->second;
				MAP_INT8_SET_UINT32::iterator iterCamp = mapCampBorn.find(info.camp);
				if (iterCamp != mapCampBorn.end())
				{
					iterCamp->second.insert(info.id);
				}
				else
				{
					SET_UINT32 setBorn;
					setBorn.clear();
					setBorn.insert(info.id);
					mapCampBorn[info.camp] = setBorn;
				}
			}
			else
			{
				MAP_INT8_SET_UINT32 mapCampBorn;
				mapCampBorn.clear();
				SET_UINT32 setBorn;
				setBorn.clear();
				//
				setBorn.insert(info.id);
				mapCampBorn[info.camp] = setBorn;
				m_mapCampBorn[info.scene] = mapCampBorn;
			} 
		}
	}

	return true;
}